import React from 'react';
import {Switch,Route,Redirect} from 'react-router-dom';
import {RouteItem} from './router.d';


interface Iprops{
    routes:RouteItem[]
}

const RouterView:React.FC<Iprops> = function({routes}){
    let routeArr = routes.filter(item => item.component);
    let redireactArr = routes.filter(item => item.redirect);
    return <Switch>
        {
            routeArr.map((item:RouteItem) => <Route path={item.path} render={(props) => {
                return <item.component {...props} routes={item.children}></item.component>
            }} key={item.path}></Route>)
        }
        {
            redireactArr.map((item:RouteItem) => <Redirect key={item.path} from={item.path} to={(item.redirect as string)}></Redirect>)
        }
    </Switch>
}

export default RouterView